package cn.jiakeyun.umeng_vertify

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.google.gson.Gson
import com.umeng.commonsdk.stateless.UMSLEnvelopeBuild.mContext
import com.umeng.umverify.UMVerifyHelper
import com.umeng.umverify.listener.UMActivityResultListener
import com.umeng.umverify.listener.UMCustomInterface
import com.umeng.umverify.listener.UMPreLoginResultListener
import com.umeng.umverify.listener.UMTokenResultListener
import com.umeng.umverify.view.UMAbstractPnsViewDelegate
import com.umeng.umverify.view.UMAuthRegisterViewConfig
import com.umeng.umverify.view.UMAuthRegisterXmlConfig
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.lang.Exception

class UVerifyManager : BaseManager() {

    private var helper: UMVerifyHelper? = null

    private var umTokenListener: UMTokenResultListener? = null
    private var uMPreLoginResultListener: UMPreLoginResultListener? = null
    private var uMAbstractPnsViewDelegate: JKUMAbstractPnsViewDelegate? = null
    private var dismissListener: DismissListener? = null

    override fun checkMethod(
        call: MethodCall,
        result: MethodChannel.Result,
        context: Context
    ): Boolean {
        val hasMethod = when (call.method) {
            MethodName.initHelper -> {
                if (umTokenListener == null) {
                    throw  Exception("please call initUMTokenResultListener")
                }

                helper = UMVerifyHelper.getInstance(context, umTokenListener)


                helper?.setUIClickListener { s, context, s2 ->
                    if (s == "700004") {

                    }else if(s == "700002"){
                        var data = Gson().fromJson<CheckData>(s2,CheckData::class.java)
                        if(data.isChecked){
                            helper?.hideLoginLoading()
                            UVerifyManagerConfig.loginListener?.login()
                        }
                    }else if(s == "700003"){
                        var data = Gson().fromJson<CheckData>(s2,CheckData::class.java)
                        UVerifyManagerConfig.checkBoxCheckListener?.check(data.isChecked);

                    }
                    Log.e("setUIClickListener", "a:$s,b:$s2")
                }
                result.success(true)
                true
            }
            MethodName.setAuthSDKInfo -> {
                val getSecret = getMethodParam(call, "secret", "")
                helper?.setAuthSDKInfo(getSecret)
                result.success(true)
                true
            }
            MethodName.checkEnvAvailable -> {
                val type = getMethodParam(call, "type", 1)
                helper?.checkEnvAvailable(type)
                result.success(true)
                true
            }
            MethodName.setLoggerEnable -> {
                val isDebug = getMethodParam(call, "isDebug", true)
                helper?.setLoggerEnable(isDebug)
                result.success(true)
                true
            }
            MethodName.getVerifyToken -> {
                val timeOut = getMethodParam(call, "timeOut", 5000)

                helper?.getVerifyToken(timeOut)
                result.success(true)
                true
            }
            MethodName.accelerateLoginPage -> {
                val overdueTime = getMethodParam(call, "timeOut", 5000)
                helper?.accelerateLoginPage(overdueTime, uMPreLoginResultListener)
                result.success(true)
                true
            }
            MethodName.getLoginToken -> {

                val timeOut = getMethodParam(call, "timeOut", 5000)
                helper?.getLoginToken(context, timeOut)
                result.success(true)
                true
            }
            MethodName.quitLoginPage -> {
                helper?.quitLoginPage()
                result.success(true)
                true
            }
            MethodName.hideLoginLoading -> {
                helper?.hideLoginLoading()
                helper?.hideLoginLoading()
                result.success(true)
                true
            }
            MethodName.addAuthRegisterXmlConfig -> {
                helper?.removeAuthRegisterXmlConfig()
                helper?.removeAuthRegisterViewConfig()
                initViewDelegate(UVerifyManagerConfig.delete)
                val config =
                    UMAuthRegisterXmlConfig.Builder()
                        .setLayout(UVerifyManagerConfig.intLayoutId!!, uMAbstractPnsViewDelegate)
                        .build()
                helper?.addAuthRegisterXmlConfig(config)
                helper?.setAuthUIConfig(UVerifyManagerConfig.uiConfigure)

                result.success(true)
                true
            }
            MethodName.loginField -> {
                UVerifyManagerConfig.loginListener?.loginField()
                true
            }

            else -> false
        }
        return hasMethod
    }

    fun dismissVerify() {
        helper?.hideLoginLoading()
        helper?.quitLoginPage()
        dismissListener?.dismiss()
    }

    private object MethodName {
        val initHelper = "initVerify"
        val setAuthSDKInfo = "setAuthSDKInfo"
        val checkEnvAvailable = "checkEnvAvailable"
        val setLoggerEnable = "setLoggerEnable"
        val getVerifyToken = "getVerifyToken"
        val accelerateLoginPage = "accelerateLoginPage"//一键登录预取号
        val getLoginToken = "getLoginToken"//一键登录预取号
        val quitLoginPage = "quitLoginPage"//退出授权页
        val hideLoginLoading = "hideLoginLoading"//退出授权页
        val addAuthRegisterXmlConfig = "addAuthRegisterXmlConfig"//一键登录页面添加由XML的自定义控件
        val addAuthRegistViewConfig = "addAuthRegistViewConfig"//一键登录添加自定义控件
        val loginField = "loginField"//一键登录添加自定义控件
    }

    /**
     * 设置友盟回调
     *
     * */
    fun initUMTokenResultListener(umTokenListener: UMTokenResultListener) {
        this.umTokenListener = umTokenListener
    }

    fun initUMPreLoginResultListener(uMPreLoginResultListener: UMPreLoginResultListener) {
        this.uMPreLoginResultListener = uMPreLoginResultListener
    }

    fun initDismissListener(dismissListener: DismissListener){
        this.dismissListener= dismissListener
    }


    /**
     * 设置UI事件代理类
     * @param delegate 代理类
     * */
    private fun <T : JKUMAbstractPnsViewDelegate> initViewDelegate(delegate: T?) {
        this.uMAbstractPnsViewDelegate = delegate
    }

    fun dispose() {}

    interface LoginListener{
        fun login()
        fun loginField()
    }

    interface DismissListener {
        fun dismiss()
    }

}